<template>
  <el-table ref="tableRef" v-loading="listLoading" border :data="list" @selection-change="setSelectRows">
    <el-table-column type="selection" width="38" />
    <el-table-column align="center" label="序号" width="55">
      <template #default="{ $index }">
        {{ $index + 1 }}
      </template>
    </el-table-column>

    <!-- 自定义开始 -->
    <el-table-column align="center" label="姓名" min-width="80" prop="name" />
    <el-table-column align="center" label="职位" min-width="120" prop="basic_position_T_position_id.name" />
    <el-table-column align="center" label="部门" min-width="120" prop="department_T_department_id.name" />
    <el-table-column align="center" label="用户名" min-width="80" prop="username" />
    <el-table-column align="center" label="工号" min-width="80" prop="work_number" />
    <el-table-column align="center" label="花名" min-width="80" prop="alias" />
    <el-table-column align="center" label="性别" min-width="80" prop="sex" />
    <el-table-column align="center" label="生日" min-width="80" prop="birthday" />
    <el-table-column align="center" label="年龄" min-width="80" prop="age" />
    <!-- <el-table-column align="center" label="地址" min-width="80" prop="address" /> -->
    <el-table-column align="center" label="入职日期" min-width="80" prop="in_date" />
    <!-- <el-table-column align="center" label="实习结束日期" min-width="80" prop="internship_end_date" /> -->
    <!-- <el-table-column align="center" label="开工资方式及账号" min-width="80" prop="wages" /> -->
    <!-- <el-table-column align="center" label="备注" min-width="80" prop="remark" /> -->
    <!-- 自定义结束 -->

    <el-table-column align="center" label="操作" width="150">
      <template #default="{ row }">
        <el-button v-if="!is_recycle" text type="primary" @click="edit(row)">编辑</el-button>
        <el-button v-if="!is_recycle" text type="danger" @click="del(row)">离职</el-button>

        <el-button v-if="is_recycle" text type="primary" @click="recovery(row)">恢复</el-button>
        <!-- <el-button v-if="is_recycle" text type="danger" @click="com_del(row)">彻底删除</el-button> -->
      </template>
    </el-table-column>
    <template #empty>
      <el-empty class="vab-data-empty" description="暂无数据" />
    </template>
  </el-table>
</template>

<script lang="ts" setup>
import { api_detail, api_del, api_delChecked, api_com_del, api_com_delChecked, api_recovery, api_recoveryChecked } from '/@/api/obj'

defineOptions({
  name: 'ObjTable',
})
const props = defineProps({
  is_recycle: {
    type: Boolean,
    default: false,
  },
  config_table: {
    type: Object,
    default: {},
  },
  list: {
    type: Array,
    default: [],
  },
  listLoading: {
    type: Boolean,
    default: false,
  },
})
const tableRef = ref<any>(null)
const $baseConfirm = inject<any>('$baseConfirm')
const $baseMessage = inject<any>('$baseMessage')

const emit = defineEmits(['getData', 'edit'])

const selectRows = ref<any>([])
const total = ref<any>(0)
//通用编辑
const edit = (row = {}) => {
  api_detail(props.config_table.tablename, { id: row.id }).then((res: any) => {
    console.log(res)
    //editRef.value.showEdit(res)
    emit('edit', res)
  })
}
//通用普通删除
const del = (row: any) => {
  console.log(props)
  if (row.id) {
    $baseConfirm('您确定要删除当前项吗', null, async () => {
      const { msg }: any = await api_del(props.config_table.tablename, { id: row.id })
      $baseMessage(msg, 'success', 'hey')
      // await getData()
      emit('getData')
    })
  } else {
    if (selectRows.value.length > 0) {
      const ids = selectRows.value.map((item: { id: any }) => item.id).join()
      $baseConfirm('您确定要删除选中项吗', null, async () => {
        const { msg }: any = await api_delChecked(props.config_table.tablename, { ids: ids })
        console.log(msg)
        $baseMessage(msg, 'success', 'hey')
        emit('getData')
      })
    } else {
      $baseMessage('您未选中任何行', 'warning', 'hey')
    }
  }
}
//通用彻底删除
const com_del = (row: any) => {
  if (row.id) {
    $baseConfirm('您确定要彻底删除当前项吗', null, async () => {
      const { msg }: any = await api_com_del(props.config_table.tablename, { id: row.id })
      $baseMessage(msg, 'success', 'hey')
      emit('getData')
    })
  } else {
    if (selectRows.value.length > 0) {
      const ids = selectRows.value.map((item: { id: any }) => item.id).join()
      $baseConfirm('您确定要删除选中项吗', null, async () => {
        const { msg }: any = await api_com_delChecked(props.config_table.tablename, { ids: ids })
        console.log(msg)
        $baseMessage(msg, 'success', 'hey')
        emit('getData')
      })
    } else {
      $baseMessage('您未选中任何行', 'warning', 'hey')
    }
  }
}
//通用恢复操作
const recovery = (row: any) => {
  if (row.id) {
    $baseConfirm('您确定要恢复当前项吗', null, async () => {
      const { msg }: any = await api_recovery(props.config_table.tablename, { id: row.id })
      $baseMessage(msg, 'success', 'hey')
      emit('getData')
    })
  } else {
    if (selectRows.value.length > 0) {
      const ids = selectRows.value.map((item: { id: any }) => item.id).join()
      $baseConfirm('您确定要恢复选中项吗', null, async () => {
        const { msg }: any = await api_recoveryChecked(props.config_table.tablename, { ids: ids })
        console.log(msg)
        $baseMessage(msg, 'success', 'hey')
        emit('getData')
      })
    } else {
      $baseMessage('您未选中任何行', 'warning', 'hey')
    }
  }
}
defineExpose({
  del,
  recovery,
  com_del,
})
//通用设置选中行
const setSelectRows = (value: string) => {
  selectRows.value = value
}
//通用选中
onActivated(() => {
  tableRef.value.doLayout()
})
</script>
<style scoped>
.z-col {
  width: 150px;
  padding: 5px;
}
</style>
